#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <vector>
#include <stdio.h>
#include <sys/types.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "myxmlwrap.h"

using namespace std;
#define LOG_ERROR_VA printf

int main(int argc, char **argv)
{
	if (argc != 2) {
		cout << "no configure file !" << endl;
		return -1;
	}

	//初始化
	xmlInitParser();
	lightxquery xqry;
	if (!xqry.load(argv[1])) {
		cout << "load config xml file error, please check " << argv[1] << endl;
		return -1;
	}

	//读个数
	int num_dispatch_server = xqry.how_many("/dumpcalc/nodes/dispatch");
	int num_calc_server = xqry.how_many("/dumpcalc/nodes/calc");
	cout << "num_dispatch_server: " << num_dispatch_server << endl;
	cout << "num_calc_server: " << num_calc_server << endl;

	//读整数
	int io_read_timeout, io_write_timeout;
	if (xqry.get_attr_value("/dumpcalc/public/time", "io_read", io_read_timeout) != 0) {
		LOG_ERROR_VA("io_read timeout configure error");
		return -1;
	}
	cout << "io_read_timeout: " << io_read_timeout << endl; 

	//读字符串
	string path, log_level;
	if (xqry.get_attr_value("/dumpcalc/public/log[@path]", "path", path) != 0) {
		cout << "log path configure error" << endl;
		return -1;
	}
	if (xqry.get_attr_value("/dumpcalc/public/log[@level]", "level", log_level) != 0) {
		cout << "log level configure error" << endl;
		return -1;
	}
	cout << "log_path: " << path << "; log_level: " << log_level << endl;


	//write file
	int file;
	file = open("conf_bak.xml", O_RDWR, O_CREAT);
	if(file <= 0){
		cerr << "open file error" << endl;
		return -1;
	}
	char *msg = "<dumpcalc>\n<public>\n<thread number_of_work_threads=\"7\" number_of_dispatch_threads=\"30\"/>\n<time io_read=\"10000\" io_write=\"10000\" />\n<log path=\"./logs\" level=\"DEBUG\" />\n</public>\n</dumpcalc>\n";
	write(file, msg, strlen(msg));
	close(file);
	//write over

	return 0;
}




